package src.view;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


import src.driver.Driver;
import src.event.events.ConsoleUpdateEvent;
import src.event.events.SelectionUpdateEvent;
import src.model.gameWorld.GameDataObject;
import src.model.gameWorld.Position;
import src.view.ViewHandler.DisplayType;

/**
 * <b>Overview</b>
 * The MainWindow is the interface between the game and the user.  This window will receive input from the user and display the appropriate representation of the game's state.
 *
 * <b>Responsibilities</b>
 * Receive mouse and keyboard input from the user for the Controller and display the game's state to the user as required.
 *
 * <b>Collaborators</b>
 * Viewport - Subcontractor: each Viewport is required to display itself as appropriate to the MainWindow.
 *
 * <b>Implementor</b> - Bruce
 * <b>Tester</b> - Tim
 *
 * @author Bruce
 */
public class MainWindow extends JFrame implements Drawable, ComponentListener
{
	/**
	 * The serial ID.
	 */
	private static final long serialVersionUID = 205236287877558310L;

	/**
	 * The off-screen image used as a buffer.
	 */
	private BufferedImage buffer;

	/**
	 * the graphics context for the off-screen image.
	 */
	private Graphics buffGraph;

	/**
	 * Creates a new MainWindow.
	 */
	public MainWindow()
	{
		setResizable(false);
		int dimX = 800, dimY = 600;
		setSize(dimX, dimY);
		buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		buffGraph = buffer.getGraphics();

		try{setIconImage(ImageIO.read(new File("images/landshark.png")));}catch(Exception e){}

		setVisible(true);
		setTitle("Scarlet Land Sharks");
		int x = getGraphicsConfiguration().getBounds().width / 2 - dimX / 2;
		int y = getGraphicsConfiguration().getBounds().height / 2 - dimY / 2;
		setLocation(x, y);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		addComponentListener(this);

//		Timer t = new Timer(200, new paintTimer(this));
//		t.start();
	}

	private class paintTimer implements ActionListener{
		MainWindow mw;
		public paintTimer(MainWindow mw){
			this.mw=mw;
		}
		public void actionPerformed(ActionEvent e){
			mw.paint(mw.getGraphics());
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.Container#paint(java.awt.Graphics)
	 */
	@Override
	public void paint(Graphics g)
	{
		if(buffer != null)
			g.drawImage(buffer, 0, 0, null);
	}

	/* (non-Javadoc)
	 * @see javax.swing.JFrame#update(java.awt.Graphics)
	 */
	@Override
	public void update(Graphics g)
	{
		g.drawImage(buffer, 0, 0, null);
	}

	public void draw(Image image, int x, int y, int width, int height)
	{
		buffGraph.drawImage(image, x + 4, y + 23, width, height, this);
		update(getGraphics());
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
	 */
	public void componentHidden(ComponentEvent e)
	{}

	/* (non-Javadoc)
	 * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
	 */
	public void componentMoved(ComponentEvent e)
	{}

	/* (non-Javadoc)
	 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
	 */
	public void componentResized(ComponentEvent e)
	{
		buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		buffGraph = buffer.getGraphics();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
	 */
	public void componentShown(ComponentEvent e)
	{}

	/**
	 * Test program.
	 *
	 * @param args the Strings to output.
	 */
	public static void main(String[] args)
	{
//		Splash.show(3500);
		Driver.initGame();

		String [] list = {"Pete fell overboard and sustained 32 damage.",
				"John shot Pete in the head for 13 damage.",
				"Pete has sustained critical damage!",
				"Pete's vessel has sunk.",
				"John is victorious and claims 4 Rum.",
				"John wins the match by outdrinking his opponents.",
				"Yay, John!!"};

		for(String s : list)
			ViewEventHandler.fireEvent(new ConsoleUpdateEvent(s));

		Position [] selection = new Position [] {new Position(1, 1)};
		ViewEventHandler.fireEvent(new SelectionUpdateEvent(selection));
	}
}